import { Color, color } from "cc";
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { TableItemConf } from "../../common/table/TableItemConf";

/** 数据层对象 */
@ecs.register('ItemModel')
export class ItemModelComp extends ecs.Comp {
      /** 提供 MVVM 组件使用的数据 */
      public vm: any = {
            path: "game/item/texture/coin",
            colorPath: "res/ui_common/pic_changkuang_5",
            fangkuangPath: "res/ui_common/item_fangkuang_4",
            num: 0,
      };

      private nameColorList: Color[] = [color(146, 221, 129, 255), color(105, 228, 243, 255), color(199, 0, 208, 255), color(227, 118, 29, 255), color(187, 7, 43, 255)]
      public nameColor: Color = null!;

      get id(): any {
            return this.table.id;
      }
      set id(value: any) {
            if (this.table == null) this.table = new TableItemConf();
            this.table.init(value);
            this.num = 0;
            this.times = 0;
            if (this.table.item_type == 5) {
                  //武将
                  this.path = `res/icons/heroes/${this.table.nick_name}`;
            } else if (this.table.item_type == 3) {
                  //碎片
                  this.path = `res/icons/small_heroes/${this.table.nick_name}`;
            } else {
                  this.path = `res/icons/items/${this.table.nick_name}`;
            }
            this.vm.colorPath = `res/ui_common/pic_changkuang_${this.table.color}`
            this.vm.fangkuangPath = `res/ui_common/item_fangkuang_${this.table.color}`
            this.nameColor = this.nameColorList[this.table.color - 1];

      }
      table: TableItemConf = null!;
      /** 道具数量 */
      private _num: number = 0;
      public get num(): number {
            return this._num;
      }
      public set num(v: number) {
            this._num = v;
            this.vm["num"] = v;
      }

      /** 倍数，只有点金时候有倍数 */
      private _times: number = 0;
      public get times(): number {
            return this._times;
      }
      public set times(v: number) {
            this._times = v;
            this.vm["times"] = v;
      }

      /** 物品图片路径 */
      private _path: string = "";
      public get path(): string {
            return this._path
      }
      public set path(value: string) {
            this._path = value;
            this.vm["path"] = value;
      }
      /** 数据层组件移除时，重置所有数据为默认值 */
      reset() {
            this._num = 0;
            this._times = 0;
            this._path = "";
            for (var key in this.vm) {
                  delete this.vm[key];
            }
      }
}